/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.spi.web;

import com.example.spi.service.SpiService;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
 */
@RestController
public class BasicController {

    @GetMapping("/spi")
    public void awareSpi(String spiName) {
        if(spiName == null || spiName.isEmpty()) {
            throw new RuntimeException("spiName can not be null");
        }
        System.out.println("do spi service start");
        List<SpiService> spiServices = SpringFactoriesLoader.loadFactories(SpiService.class, this.getClass().getClassLoader());
        for (SpiService spiService : spiServices) {
            if(spiName.equals(spiService.getClass().getName())) {
                System.out.println("get spi,current spi is:" + spiService.getClass().getName());
                spiService.doSomething();
                break;
            }
        }
        System.out.println("do spi service end");
    }
}
